package com.kmzx.gen.signs.web;

import com.kmzx.gen.signs.api.FileApi;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;

/**
 * @Project gen_sign
 * @Author: carol
 * @Date: 2020/7/21-18:09
 */

@Controller
public class ReceiptDataController {
    private static Logger logger = LoggerFactory.getLogger(ReceiptDataController.class);


    @Autowired
    FileApi fileApi;

    @RequestMapping(value = "receipt_data")
    @ResponseBody
    public String receiptData(HttpServletRequest request) {
        try {
            String fileName = request.getHeader("fileName");
            String md5 = request.getHeader("md5");
            InputStream inputStream = request.getInputStream();
            String postData = IOUtils.toString(inputStream, "UTF-8");
            logger.info("postData = " + postData);
            int dist = fileApi.pushFileToDist(fileName, postData);
            if (dist == 1) {
                return "{\"code\":1,\"msg\":\"OK\"}";
            } else {
                return "{\"code\":0,\"msg\":\"文件不存在\"}";
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "{\"code\":0,\"msg\":\"OK\"}";

    }


    @RequestMapping(value = "receipt_data2")
    @ResponseBody
    public String receiptDataNow(HttpServletRequest request) {
        try {
            String md5 = request.getHeader("md5");
            String fileName = request.getHeader("fileName");
            String guid = request.getHeader("guid");
            InputStream inputStream = request.getInputStream();
            String postData = IOUtils.toString(inputStream, "UTF-8");
            logger.info("postData = " + postData);
            int dist = fileApi.pushFileToDistMulti(guid, fileName, postData);
            if (dist == 1) {
                return "{\"code\":1,\"msg\":\"OK\"}";
            } else {
                return "{\"code\":0,\"msg\":\"文件不存在\"}";
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "{\"code\":0,\"msg\":\"OK\"}";

    }

    @RequestMapping(value = "receiptDataCode/{orderCode}")
    @ResponseBody
    public String receiptDataNow2(HttpServletRequest request, @PathVariable("orderCode") String orderCode) {
        try {
            String md5 = request.getHeader("md5");
            String fileName = request.getHeader("fileName");
            String guid = request.getHeader("guid");
            InputStream inputStream = request.getInputStream();
            String postData = IOUtils.toString(inputStream, "UTF-8");
            logger.info("postData = " + postData);
            int dist = fileApi.pushFileToTJSea(guid, fileName, postData, orderCode);
            if (dist == 1) {
                return "{\"code\":1,\"msg\":\"OK\"}";
            } else {
                return "{\"code\":0,\"msg\":\"文件不存在\"}";
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "{\"code\":0,\"msg\":\"OK\"}";

    }

}
